package com.slz3.business.module.memberManagement.Feedback.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.memberManagement.Feedback.domain.Feedback;
import com.slz3.business.module.memberManagement.Feedback.domain.FeedbackFactory;
import com.slz3.business.module.memberManagement.Feedback.steward.FeedbackSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class FeedbackServiceImpl implements FeedbackService {
    @Override
    public Feedback save(Feedback Feedback) {
        return Repositorys.feedback.save(FeedbackFactory.initBaseFields(Feedback));
    }
    @Override
    public Page<Feedback> page(MultiValueMap<String, String> params) {
        Page<Feedback> FeedbackPage = Repositorys.feedback.findAll(
                FeedbackSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return FeedbackPage;
    }


    @Override
    public Feedback modify(Feedback FeedbackNew) {
        return Repositorys.feedback.saveAndFlush(FeedbackFactory.initBaseFields(FeedbackNew));
    }

    @Override
    public Optional<Feedback> single(UUID uuid) {
        return Repositorys.feedback.findById(uuid);
    }

    @Override
    public List<Feedback> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.feedback.findAllById(uuids);
    }

    @Override
    public List<Feedback> delete(List<Feedback> FeedbackList) {
        FeedbackList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            FeedbackFactory.initBaseFields(item);
        });
        return Services.feedbackTransactional.batchModify(FeedbackList);
    }

}
